package com.bhkj.pdjhbgnewproject.core.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.bhkj.pdjhbgnewproject.R;
import com.bhkj.pdjhbgnewproject.common.utils.Tools;
import com.bhkj.websocketlibrary.socketio.ISocketIoCallback;
import com.bhkj.websocketlibrary.socketio.SocketIoManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        testWs();

//        ImageView iv = findViewById(R.id.iv_test);
//        iv.setImageResource(R.drawable.x2);
//        DeviceResponse.getInstance().execute(new IDeviceResCallback() {
//            @Override
//            public void onStartRes() {
//                LogUtils.e("onStartRes");
//            }
//
//            @Override
//            public void onRes(final DatagramPacket res) {
//                new Thread(){
//                    @Override
//                    public void run() {
//                        super.run();
//                        LogUtils.e("onRes:"+res.getAddress().getHostName());
//                    }
//                };
//            }
//
//            @Override
//            public void onResException(String exceptionMsg) {
//                LogUtils.e("onResException:"+exceptionMsg);
//            }
//        });
//       findViewById(R.id.tv_activity_test).setOnClickListener(new View.OnClickListener() {
//           @Override
//           public void onClick(View v) {
//               DeviceFound.getInstances().searchDevice(new ISearchDeviceCallback() {
//                   @Override
//                   public void onSearchStart() {
//                       LogUtils.e("onSearchStart");
//                   }
//
//                   @Override
//                   public void onFoundNewDevices(List<DeviceBean> devices) {
//                       for (int i = devices.size() - 1; i >= 0; i--) {
//                           LogUtils.d(devices.get(i).getName()+"-"+devices.get(i).getPort());
//                       }
//
//                   }
//
//                   @Override
//                   public void onFoundException(String exceptionMsg) {
//                       LogUtils.e("onFoundException:"+exceptionMsg);
//                   }
//
//                   @Override
//                   public void onFinishDevice() {
//                       LogUtils.e("onFinishDevice");
//                   }
//               });
//           }
//       });
        //静默安装
//        ServiceUtils.startService(ObtainPdInfoService.class);
//        final TextView test = findViewById(R.id.tv_activity_test);
//        CallbackManager.getInstance().addCallback("error", new IGlabolCallback<String>() {
////            @Override
////            public void executeCallback(final String error) {
////
////                runOnUiThread(new Runnable() {
////
////                    @Override
////                    public void run() {
////
////                        test.setText(error);
////                    }
////                });
////            }
////        });
////
////        CallbackManager.getInstance().addCallback("success", new IGlabolCallback<String>() {
////            @Override
////            public void executeCallback(String success) {
////                test.setText(success);
////            }
////        });
//        ServiceUtils.startService(ObtainPdInfoService.class);
//
//        findViewById(R.id.tv_activity_stop).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                IGlabolCallback callback = CallbackManager.getInstance().getCallback(Contacts.PUSH_LOGOUT);
//                if (callback!=null){
//                    callback.executeCallback("");
//                }
//            }
//        });
//
//        findViewById(R.id.tv_activity_start).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                IGlabolCallback callback = CallbackManager.getInstance().getCallback(Contacts.PUSH_LOGIN);
//                if (callback!=null){
//                    callback.executeCallback("");
//                }
//            }
//        });
    }

    private void testWs() {
//        final WsClient client = WsClient.getInstance();
//
//        client.addOnCallback(new WsClient.IWsCallback() {
//            @Override
//            public void onMessage(String msg) {
//                ToastUtils.showShort(msg);
//            }
//        });
        final SocketIoManager instance = SocketIoManager.getInstance();
        instance.addCallback(new ISocketIoCallback() {
            @Override
            public void onLogin(final String login) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LogUtils.d("onLogin:" + getResMsg(login));
                    }
                });
            }

            @Override
            public void onLogout(String logout) {
                LogUtils.d("onLogout:" + getResMsg(logout));

            }

            @Override
            public void onWindowStatus(String status) {
                LogUtils.d("status:" + getResMsg(status));

            }

            @Override
            public void onOther(String res, String otherType) {
                LogUtils.d("res:" + getResMsg(res) + "-" + otherType);

            }
        });
        findViewById(R.id.tv_activity_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                client.connect();
                instance.toLogin(NetworkUtils.getIPAddress(true));
            }
        });

        findViewById(R.id.tv_activity_stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                client.close();

            }
        });
//        TextView tvContent = findViewById(R.id.iv_activity_test_content);
//        tvContent.setText();

    }

    private String getResMsg(String message) {
        return Tools.AESUtil.decrypt(message, getString(R.string.mm));
    }
}
